//
//  AppDelegate.m
//  ShouYi
//
//  Created by yurui on 16/2/18.
//  Copyright © 2016年 yurui. All rights reserved.
//

#import "AppDelegate.h"
#import "BaseTabBarViewController.h"
#import "UMSocial.h"
#import "UMSocialWechatHandler.h"
#import "UMSocialQQHandler.h"
#import "WXApi.h"

@interface AppDelegate ()<NSURLConnectionDataDelegate>

@property(nonatomic,strong) AppDelegate *delegate;
@property (nonatomic,retain)NSMutableData *data;

@end

@implementation AppDelegate
@synthesize isLogin;
@synthesize isThirdLogin;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    BaseTabBarViewController *baseVC = [[BaseTabBarViewController alloc]init];
    self.window.rootViewController = baseVC;
 
    //设置友盟AppKey
    [UMSocialData setAppKey:@"56cc12f3e0f55ae3700026f2"];
    //微信
    [UMSocialWechatHandler setWXAppId:kWXAppID appSecret:kWXAppKey url:nil];
    [WXApi registerApp:kWXAppID];
    
    
    [kUserDefault setObject:@"123456" forKey:@"CFUUID"];
    
    //获取ip
    [self getIp];
    
    
    return YES;
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    BOOL result = [UMSocialSnsService handleOpenURL:url];
    if (result == FALSE) {
        
    }
    return result;
}
//异步get 请求 ip地址
- (void)getIp {
    
    NSURL *url = [NSURL URLWithString:@"http://1212.ip138.com/ic.asp"];
    
    //创建一个请求
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
    // 设置一个请求方式
    [request setHTTPMethod:@"Get"];
    
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
        
        NSString *ipStr = [[NSString alloc] initWithData:data encoding:-2147482591];
        if ([ipStr length] > 30) {
            NSArray *tempLeft = [ipStr componentsSeparatedByString:@"["];
            NSArray *tempRight = [tempLeft[1] componentsSeparatedByString:@"]"];
            [[NSUserDefaults standardUserDefaults] setObject:tempRight[0] forKey:@"IP"];
            NSLog(@"ip地址为  %@",tempRight[0]);
            
        }
    }];
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
